package com.woniuxy.dr_mall.config;

import com.woniuxy.dr_mall.filter.MyPermissionsAuthorizationFilter;
import com.woniuxy.dr_mall.shiro.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 张哲轩
 * @version 1.0.0
 * @ClassName ShiroConfig.java
 * @createTime 2021年07月29日 15:29:00
 */
@Configuration
@Slf4j
public class ShiroConfig {

    @Bean
    public DbRealm realm() {
        DbRealm realm = new DbRealm();
//        realm.setCredentialsMatcher(credentialsMatcher());
//        realm.setCacheManager(ehCacheManager());
        return realm;
    }

    @Bean
    public Realm jwtRealm() {
        JwtRealm realm=new JwtRealm();
        return realm;
    }

//    @Bean
//    public HashedCredentialsMatcher credentialsMatcher(){
//        HashedCredentialsMatcher hashedCredentialsMatcher=new HashedCredentialsMatcher();
//        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
//        hashedCredentialsMatcher.setHashIterations(1024);
//        return hashedCredentialsMatcher;
//    }

    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        MultiRealmAuthenticator multiRealmAuthenticator = new MultiRealmAuthenticator();
        MultiRealmAuthenticator authenticator=new MultiRealmAuthenticator();
        //设置realm
        authenticator.setRealms(Arrays.asList(realm(),jwtRealm()));
        //设置认证器
        securityManager.setAuthenticator(authenticator);
        //设置授权器
        securityManager.setAuthorizer(realm());

        return securityManager;
    }


    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        log.debug("shiroFilterFactoryBean");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        //未认证的情况下
        shiroFilterFactoryBean.setLoginUrl("/login.html");
        //没权限的情况下
        shiroFilterFactoryBean.setUnauthorizedUrl("/login.html");
        Map<String, Filter> filters=new HashMap<>();
        filters.put("myperms",new MyPermissionsAuthorizationFilter());
        filters.put("jwt",new JwtFilter());
        shiroFilterFactoryBean.setFilters(filters);
        try {
            shiroFilterFactoryBean.setFilterChainDefinitionMap(permMapFactoryBean().getObject());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return shiroFilterFactoryBean;
    }

    @Bean
    public PermMapFactoryBean permMapFactoryBean() {
        return new PermMapFactoryBean();
    }

    //解决url后面带jsession_id问题
    @Bean
    public DefaultWebSessionManager sessionManager(){
        DefaultWebSessionManager sessionManager =new DefaultWebSessionManager();
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }
}
